package shj.blackberry.i.util;

import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 文件访问功能类
 * @author shan.hongjie
 *
 */
public class FileAccess {
	
	/**
	 * 从项目data文件夹下读取文件为字符串
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public static String getStringFromDataDirFile(String filename){
		InputStream is = null;
		InputStreamReader isr = null;
		int bufSize = 1024;
		char[] c = new char[bufSize];
		StringBuffer sb = new StringBuffer();
		try {
			is = FileAccess.class.getResourceAsStream(Constants.DATA_DIR+filename);
			isr = new InputStreamReader(is,"UTF-8");
			int stat = isr.read(c);
			while(stat != -1){
				if(stat==bufSize){
					sb.append(c);
				}else{
					sb.append(c, 0, stat);
					break;
				}
			}
		} catch (Exception e) {
			return "读取"+filename+"的文本出错";
		} finally{
			if(isr!=null){
				try {
					isr.close();
				} catch (Exception e) {
				}
			}
			if(is!=null){
				try {
					is.close();
				} catch (Exception e) {
				}
			}
		}
		return sb.toString();
	}
}
